Ajax选择具有预选值

时间:2015-12-12 11:42:32

标签: javascript php ajax select

我的ajax脚本有点问题,当你自己在下拉列表中选择值时脚本运行正常。但是当你点击提交时,它会获取值并将其置于GET中。

当你转到“http://script.domain?prov=1”(它在下拉列表中触发选定的值)时,ajax脚本无效(我认为是因为。(更改),当它的值被预先选中时它没有改变所以脚本没有反应。)

所以我需要脚本在加载页面时自动运行并检查#prov dropbox值。

这是链接https://jsfiddle.net/gfzd5aa1/1/

HTML

<form action="" method="get">
<label for="prov">Provincia: </label>
<select name="prov" id="prov" class="form-control">
<option value="0">Selecciona la provincia</option>
    {% for provincia in provincias %}
     <option value="{{ provincia.id }}" {% if get.prov == provincia.id    %} selected  {% endif %} > {{ provincia.name }} </option>
{% endfor %}
</select>

<label for="zona">Provincia: </label>
<select name="zona" id="zona" class="form-control">

</select>
</form>

“Provincia”选择字段触发ajax填充“Zona”选择。

$('#prov').on('change', function() {
var self = $(this);

$.ajax({
    url: 'http://localhost:8888/casas/public/ajax-prov',
    type: 'GET',
    data: { id: self.val() },
    dataType: 'json',
    success: function(response){
        var $zona = $("#zona");
        $zona.empty(); // remove old options

        $("#zona").append('<option value="0">Seleciona la Zona</option>');
        $.each(response.prov, function(i, state) {

            $("#zona").append('<option value="'+ state.id +'">' + state.name + '</option>');

         });

    }

});
});

我的javascript技能非常基础,如果你可以帮助我让它工作,我会很感激。

1 个答案:

答案 0 :(得分:0)

您没有向获取请求发送class AccountViewController: FormViewController { let Client = Client() var users: [User]? var name: String? var email: String? required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func viewDidLoad() { super.viewDidLoad() Client.Service.getId() { users, error in if users != nil { self.users = users if self.users?.count != 0 { self.loadForm() self.name = self.users![0].name// IS NOT NIL self.email = self.users![0].email// IS NOT NIL } } else { print("error: \(error)") } } } private func loadForm() { let form = FormDescriptor() form.title = "Example Form" let section1 = FormSectionDescriptor() var row: FormRowDescriptor! = FormRowDescriptor(tag: Static.emailTag, rowType: .Email, title: "Email") row.configuration[FormRowDescriptor.Configuration.CellConfiguration] = ["textField.placeholder" : "john@gmail.com", "textField.textAlignment" : NSTextAlignment.Right.rawValue] row.value = self.email print(email) ////IS NIL section1.addRow(row) row = FormRowDescriptor(tag: Static.nameTag, rowType: .Name, title: "Name") row.configuration[FormRowDescriptor.Configuration.CellConfiguration] = ["textField.placeholder" : "Miguel Angel Ortuño", "textField.textAlignment" : NSTextAlignment.Right.rawValue] row.value = self.name print(name)///IS NILL section1.addRow(row) row = FormRowDescriptor(tag: Static.phoneTag, rowType: .Phone, title: "Phone") row.configuration[FormRowDescriptor.Configuration.CellConfiguration] = ["textField.placeholder" : "e.g. 0034666777999", "textField.textAlignment" : NSTextAlignment.Right.rawValue] section1.addRow(row) let section2 = FormSectionDescriptor() row = FormRowDescriptor(tag: Static.birthday, rowType: .Date, title: "Birthday") section2.addRow(row) row = FormRowDescriptor(tag: Static.picker, rowType: .Picker, title: "Gender") row.configuration[FormRowDescriptor.Configuration.Options] = ["F", "M", "U"] row.configuration[FormRowDescriptor.Configuration.TitleFormatterClosure] = { value in switch( value ) { case "F": return "Female" case "M": return "Male" case "U": return "I'd rather not to say" default: return nil } } as TitleFormatterClosure row.value = "M" section2.addRow(row) let section3 = FormSectionDescriptor() section3.headerTitle = "Push Notifications" row = FormRowDescriptor(tag: Static.enabled, rowType: .BooleanSwitch, title: "Enable") section3.addRow(row) let section4 = FormSectionDescriptor() section4.headerTitle = "Privacy" row = FormRowDescriptor(tag: Static.categories, rowType: .MultipleSelector, title: "Profile") row.configuration[FormRowDescriptor.Configuration.Options] = [0, 1, 2] row.configuration[FormRowDescriptor.Configuration.AllowsMultipleSelection] = true row.configuration[FormRowDescriptor.Configuration.TitleFormatterClosure] = { value in switch( value ) { case 0: return "Public" case 1: return "Friend" case 2: return "Me" default: return nil } } as TitleFormatterClosure section4.addRow(row) row = FormRowDescriptor(tag: Static.categories, rowType: .MultipleSelector, title: "Activity") row.configuration[FormRowDescriptor.Configuration.Options] = [0, 1, 2] row.configuration[FormRowDescriptor.Configuration.AllowsMultipleSelection] = true row.configuration[FormRowDescriptor.Configuration.TitleFormatterClosure] = { value in switch( value ) { case 0: return "Public" case 1: return "Friend" case 2: return "Me" default: return nil } } as TitleFormatterClosure section4.addRow(row) form.sections = [section1, section2, section3, section4] self.form = form } } 变量,而是发送prov。 试试这个:

id

编辑: 小姐理解了这个问题所以最后的回答是

...
$.ajax({
    url: 'http://localhost:8888/casas/public/ajax-prov',
    type: 'GET',
    data: { prov: self.val() },
...