English 中文(简体)
使用 jquery 将带有动态 ID 的复选框值复制到隐藏的输入值
原标题:Copying checkbox value with dynamic ID to hidden input value using jquery

我sooo... 接近但被困在一件对你来说可能非常简单的小东西上。 想法是将每个复选框的值添加到隐藏字段值中 。

当我用他们自己的变量 IDs 添加第二个(或更多表格) 时, 它就会崩溃。 我知道 jQuery 代码的第一部分有效, 但我不知道如何将 < code>loader_ id 引入这里 。

$( #fileset- + loader_id +  input ).click(updateFileField);

我认为这是我搞砸的地方,因为如果我硬代码,这里的代号就会很好用。

以下是完整的代码,也可以找到 这里

<div id="fileset-1" class="fileset">
<input type="checkbox" value="set1">
<input type="checkbox" value="set2">
<input type="checkbox" value="set3">
</div>  
File 1 <input type="text" val="" id="file-1" class="filestosend" /><br />

<div id="fileset-2" class="fileset">
<input type="checkbox" value="seta">
<input type="checkbox" value="setb">
<input type="checkbox" value="setc">
</div> 
File 2 <input type="text" val="" id="file-2" class="filestosend" />​

这里的 jQuery 部分

 function updateFileField() {

 var allVals = [];
 var loader_id = $(this).parent( div ).attr( id ).replace( fileset- ,   );
     $( #fileset-  + loader_id +   :checked ).each(function() {
       allVals.push($(this).val());
     });         
     $( #file-  + loader_id ).val(allVals);
  }
 $(function() {
     $( #fileset- + loader_id +  input ).click(updateFileField);

});

谢谢你的帮助!

最佳回答

你一直在找吗?

$(".fileset :checkbox").on("change", function() {
    var div = $(this).parent();
    var id = div.attr("id").split("-")[1];
    var values = [];
    div.find(":checked").each(function() {
        values.push(this.value);
    });
    $("#file-" + id).val(values.join(","));
});

"强力 " DEMO: http://jsfiddle.net/fXMrij/52/

问题回答

下一部分 loader_id 尚未定义 。

$(function() {
     $( #fileset- + loader_id +  input ).click(updateFileField);

});




相关问题
Duplicate a table row with UITableViewCellEditingStyleInsert?

I have an application based on the Core Data Books example, and I m coming to the conclusion that I need to give the user the ability to duplicate a row in the table - a set of data - and then let ...

Copy/Paste in Windows Forms with custom controls

I am writing a small application in C# using Windows Forms. I want to let my users copy and paste data around the application and there are some custom controls, for example one is a colour picker. ...

Copying only non-existent files in ant

I m deploying my project to a web-server to be deployed with java Web Start. However, Web Start uses modification date to figure out whether to download the resources again (by default). What I want ...

Copying one FlowDocument to Second FlowDocument

How can i copy the contents of one FlowDocument to another FlowDocument below is what i tryed foreach (var blk in fd1.Blocks) { fd2.Blocks.Add(blk); } fd1 is FlowDocument1 and fd2 is ...

emacs command to append to ring

How can I make an emacs command to copy text (to the kill ring) by appending? (Why is there no such built-in command?) Appending Kills mentions C-M-w (`append-next-kill ) which allows me to append ...

about get value from sqlite

Code Sample: NSString *str= [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 1)]; Test *t=[[Test alloc] init]; t.str=[str copy]; // why use "copy" here? [str release];

Insert into an STL queue using std::copy

I d like to use std::copy to insert elements into a queue like this: vector<int> v; v.push_back( 1 ); v.push_back( 2 ); queue<int> q; copy( v.begin(), v.end(), insert_iterator< queue&...

热门标签