开发者

jQuery dynamic form, how get the value with dynamic id's?

开发者 https://www.devze.com 2023-03-21 21:47 出处:网络
I am new in jQuery and I have to do a dynamic form with two selects, in one select I need to make that if the user choose the option let\'s say B another select appear but if he choose A then it doesn

I am new in jQuery and I have to do a dynamic form with two selects, in one select I need to make that if the user choose the option let's say B another select appear but if he choose A then it doesn't show, the problem is that I can get to every element in the form, I do the form with a PHP for and then pass the variable to the id's so it gets something like this:

<select id="edocivil<?php echo $x ?>" class="textbox" name="edocivil<?php print $x; ?>" >

$x been the number of the field, I need to get the value of that field so if it B it shows these other select that is in a div, this is how:

<div id="regimen<?php echo "$x"; ?>" style="display:none;">

and 开发者_StackOverflowwhat I do is have a hidden field with the value of $x so I can get it with jQuery but I can't do it I need help please, I leave the jQuery script and the hidden field thank you.

$(document).ready(function(){ //asÌ es como se crean las funciones jQuery. solo cree

var id_edo= $(this).next(".numero_id_edo").val();
alert(id_edo);
$("#edocivil"+id_edo).change(function(){ //se ejecuta con el evento onChange  


 var val = $("#edocivil"+id_edo).val()

if(val =="B")
{
    $("#regimen"+id_edo).show();  
}
else
{
    $("#regimen"+id_edo).hide();  
}

});//change
});//ready

the hidden field:

<input type="hidden" name="numero_id_edo"  id="numero_id_edo" value="<?php echo $x; ?>" class="numero_id_edo">

I tried to make it the most understandable way, I am really new to this of Stack Overflow


ok if i have understood well you can do it like this, first get the value of hidden field so that we have the $x

var $x = $("#numero_id_edo").val(); //here we get the value of hidden field or inother words $x

now on the basis of this we can get the selected value of dropdown

$("#edocivil"+$x).change(function(){

alert($(this).val());

});

edit

by assuming you have multiple hidden fields on the page you can get there values like

var idArr=[];

$("input:hidden").each(function(){

idArr.push($(this).val());
});

alert(idArr.length);

for (v in idArr )
{
alert(idArr[v]);
}

look at this fiddle hopefully you will get the idea http://jsfiddle.net/3nigma/hAcuF/1/

0

精彩评论

暂无评论...
验证码 换一张
取 消