c# - Getting a value by name in javascript/razor application -
i have asp application in have use javascript value of element
@for(int i=0; i< model.count; i++){ <input type="text" value="@model[i].age" name="age@(i)" /> htmlactionlink("new age","change", new { age = "age" + i.tostring()}); } i need value of each tag age : new { age = "age" + i.tostring() didn't work , have replace script javascript using document.getelementsbyname method
how can modify code task?
i add class each input element gives meaning (e.g. 'input-age'):
<input class="input-age" type="text" value="@model[i].age" name="age@(i)" /> and select of inputs class name , not need rely on dynamically assigned name view engine:
var ages = document.queryselectorall('input.input-age'); or if need find value specific age index, without adding class name:
var ageindex = 4; var age = document.queryselector('input[name="age' + ageindex + '"]').value; fyi, ie, work in versions 8 or higher because of queryselectorall function, use other methods.
if jquery on table, not change , use "starts with" selector "age" elements:
var ages = $('input[name^="age"]'); or, after adding class name:
var ages = $('.input-age'); or, hunt down value specific input:
var ageindex = 4; var age = $('input[name="age' + ageindex + '"]').val(); with last example, don't need add class name;
Comments
Post a Comment