我有一個自定義下拉列表。想要將該按鈕的值更改爲下拉列表 的選定選項。將按鈕的值動態更改爲下拉列表的所選值
這是我的視圖代碼。我嘗試了jQuery,但未成功。 請有人幫我用jQuery
<div class="col-md-5 col-sm-6">
<div class="form-group droplist">
<label for="project_category">project Categary</label><br/>
<input type="button" class="form-control" id="relig" value="<?php if(isset($projectDetails->project_category) && !empty($projectDetails->project_category)){
echo @$projectDetails->project_category;
}else{
echo "select Category";
}?>">
<div id="religions" class="dropdownmenu" style="display:none;padding-left:17px">
<?php foreach($categories as $cat){ ?>
<div id="<?php echo $cat['code'] ?>" class="maincategory">
<label><input type="radio" class="category" name="project_category" id="category" value="<?php echo $cat['code'] ?>"> <?php echo $cat['Project_classification_id']." - ".$cat['Description'] ?></label><br/>
</div>
<?php } ?>
</div>
</div>
</div>
我以上對140個下拉值,我用的foreach,notonly 3個值
這是我的jQuery。我做了所有的事情,但無法顯示選中的單選值按鈕值(標題)
$('#relig').click(function(){
$('#religions').slideToggle("fast");
});
traversed_ids = [];
$(document).on('change','.project_category',function() {
maincat = $(".project_category:checked").val();
if ($.inArray(maincat, traversed_ids) < 0) { //check element exist in array or not
traversed_ids.push(maincat); //add element to array
changeCategoryList(maincat); //call ajax
}
});
function changeCategoryList(maincategory){
$.ajax({
url: '<?php echo site_url("abcd/xyz"); ?>',
type: 'POST',
data: { maincategory:maincategory },
dataType: 'json',
success: function(data) {
$.each(data, function(key, value) {
var MoreTag='';
MoreTag += '<div id="'+value.code+'" Style="padding-left:20px" class="subcategory">';
MoreTag += '<label ><input type="radio" class="project_category" name="project_category" id="project_category" value="'+value.code+'"> '+value.Project_classification_id+' - '+value.Description+'</label><br/>';
MoreTag += '</div>';
$("#"+maincategory).append(MoreTag);
});
}
});
}
$(".maincategory").click(function (e) {
e.stopPropagation(); //to stop event bubbling
if ($(this).children('.subcategory').is(':checked')) { //check if hidden or not
$(this).children('.subcategory').hide(); //if yes hide
} else {
$('.maincategory').children('.subcategory').hide();
$(this).children('.subcategory').show(); // else show
}
});
哪裏是你的下拉代碼和jQuery代碼,同時也把你的嘗試中的問題。 – RJParikh
@ RJParikh編輯我的問題..請通過該 – Mahantesh