admin管理员组

文章数量:1424916

I have an issue with automatic name for input, I'll try to explain what i need to do. i have an id, that I get it from an external function. I need to use this numeric id to create another function like that.

var id = 10;  // this is the id (from external function)
var value = "n"+bar.toString(); // (I try to cast the id as string) 
$("input[name="+value+"]").on('change', function() { // use the cast value to name my input.
  alert($("input[name="+value+"]:checked", "#myForm").val()); 
});

When I try to do that I get undefined, but when I change the id like that var id ="10" I get the correct answer, but I have a numeric input. Please help me figure out how to solve this problem.

I have an issue with automatic name for input, I'll try to explain what i need to do. i have an id, that I get it from an external function. I need to use this numeric id to create another function like that.

var id = 10;  // this is the id (from external function)
var value = "n"+bar.toString(); // (I try to cast the id as string) 
$("input[name="+value+"]").on('change', function() { // use the cast value to name my input.
  alert($("input[name="+value+"]:checked", "#myForm").val()); 
});

When I try to do that I get undefined, but when I change the id like that var id ="10" I get the correct answer, but I have a numeric input. Please help me figure out how to solve this problem.

Share Improve this question edited Mar 24, 2015 at 11:09 Andy 63.6k13 gold badges71 silver badges98 bronze badges asked Mar 24, 2015 at 11:03 nedal jedidinedal jedidi 771 gold badge3 silver badges11 bronze badges 10
  • 2 i did't see you cast the id to string. bar.toString(); should be id.toString(); and what is n for?? – Norlihazmey Ghazali Commented Mar 24, 2015 at 11:06
  • Here, 'n' refers to...? – Viswanath Donthi Commented Mar 24, 2015 at 11:07
  • my inputs are like that : n10 , n20 .... its just a name – nedal jedidi Commented Mar 24, 2015 at 11:07
  • then use id.toString(); – Nishit Maheta Commented Mar 24, 2015 at 11:08
  • 2 How about var value = "n" + 10; There's no need to do toString() – Molda Commented Mar 24, 2015 at 11:11
 |  Show 5 more ments

3 Answers 3

Reset to default 2

Did you want something like this? This is based on an assumption that you have checkboxes within a form!

var ids = [10, 20, 30, 11, 12];
$.each(ids, function(index, val) {
    var id = val;
    var value = "n" + id; // `.toString` is not required!
    $("#myForm").find("input[name='"+value+"']").on('change', function() {
        if ($(this).is(":checked")) {
            alert( $(this).val() );
        }
    });
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myForm">
    <input type="checkbox" name="n10" value="10" />
    <input type="checkbox" name="n11" value="11" />
    <input type="checkbox" name="n12" value="12" />
</form>

use this code no need for id.toString()

var id = getId();  // this is the id (from externel function)
var value = "n" + id;
$("input[name="+value+"]").on('change', function() {
    alert($("input[name="+value+"]:checked").val());  //change this selector accordingly
});

function getId() {
    return 10;
}

here is the fiddle https://jsfiddle/rrehan/srhjwrz4/

Try below code:

var id = 10;  // this is the id (from externel function)
              var value = id.toString(); // (i try to cast the id as string) 
            console.log(value);
            $("input[name="+value+"]").on('change', function() { // use the casted value to name my input.
                  alert($("input[name="+value+"]:checked", "#myForm").val()); 
                });

Demo Link

本文标签: javascriptdynamic name for an input with jQueryStack Overflow