Use .ajax in a form to make a select secondary linkage. The second select can successfully obtain the data returned by ajax, but when you need to use the form button to submit the entire form data, the button does not respond. How is this going?
Code
<script>
$(document).ready(function(){
$('#subject').change(function(){
var objectModel = {};
var value = $(this).val();
var type = $(this).attr('id');
objectModel[type] =value;
$.ajax({
cache:false,
type:"POST",
url: "URL",
dataType:"json",
data:objectModel,
error:function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
},
success:function(data){
$("#category").empty();
var count = data.length;
var i = 0;
var b="<option>请选择</option>";
for(i=0;i<count;i++){
b+="<option value='"+data[i].id+"'>"+data[i].category+"</option>";
}
$("#category").append(b);
}
});
});
});
</script>
HTML
<form action="/admin/blogs/create" method="post">
{:token()}
<p class="form-group form-group-sm form-inline">
<label for="title" class="control-label">标题</label>
<input type="text" class="form-control" style="width: 400px;" id="title" name="title" required="required">
</p>
<p class="form-group form-group-sm form-inline">
<label for="subject"> 板块</label>
<select class="form-control" style="width: 200px;" id="subject" name="subject">
<option>请选择板块</option>
<!-- 这里是后台数据 -->
</select>
</p>
<p class="form-group form-group-sm form-inline">
<label for="category">子类</label>
<select class="form-control" style="width: 200px;" id="category" name="category">
<option>请选择</option>
<!-- 这里需要获取ajax数据 -->:w
</select>
</p>
<p class="form-group">
<label for="content">内容</label>
<textarea id="content" name="content" class="form-control" required="required" rows="12"></textarea>
</p>
<button type="submit" class="btn btn-success">新增</button>
</form>