我有一个表单,我想更改用户从<select></select>
下拉列表中选择选项的操作。
这是我的表格:
<form role="form" method="post" id="switch-customer-form" class="switch-customer-form">
{% csrf_token %}
<div class="form-group col-xs-12 floating-label-form-group controls">
<select class="form-control customers-dropdown" id="customers-dropdown" name="form_customers">
{% for c in customers %}
<option{% if customer == c %} selected="true"{% endif %}>{{ c }} - {{ c.id }}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-xs-12 floating-label-form-group controls">
<input type="submit" value="View Calendar" class="btn btn-primary btn-sm">
</div>
</form>
这是我的(非工作)JS:
$("#customers-dropdown").change(function() {
var action1 = "/ac/calendar/";
var action2 = "/2014/12/";
var customer = $(this).val();
var customer_split = customer.split("-");
var customer_id = customer_split[1].trim();
var final_action = action1 + customer_id + action2;
alert(final_action);
$("#switch-customer-form").attr("action", final_action);
});
我遇到的问题是将.trim()
添加到customer_id
变量初始化。出于某种原因,在添加此功能时,它不再更改表单的action
。如果没有.trim()
,它确实会正确地更改操作,但是存在无关的空格会导致链接无法指向相应的页面。
.trim()
中是否有某些内容导致此问题?有没有更理想的方法从该变量中删除空格?谢谢你的帮助。
更新(为清晰起见):
这是我成功更改表单操作的唯一JS,尽管它仍然包含一个额外的空格:
$("#customers-dropdown").change(function() {
var action1 = "/ac/calendar/";
var action2 = "/2014/12/";
var customer = $(this).val();
var customer_split = customer.split("-");
var customer_id = customer_split[1];
var final_action = action1 + customer_id + action2;
alert(final_action);
$("#switch-customer-form").attr("action", final_action);
});
这不会为<option>
标记添加值。这样做后,我遇到了同样的问题,它没有更新表单操作,只是发布到当前页面。
答案 0 :(得分:1)
这样做会不会更容易? (使用id作为选择选项的值)
<form role="form" method="post" id="switch-customer-form" class="switch-customer-form">
{% csrf_token %}
<div class="form-group col-xs-12 floating-label-form-group controls">
<select class="form-control customers-dropdown" id="customers-dropdown" name="form_customers">
{% for c in customers %}
<option{% if customer == c %} selected="true"{% endif %} value="{{c.id}}">{{ c }} - {{ c.id }}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-xs-12 floating-label-form-group controls">
<input type="submit" value="View Calendar" class="btn btn-primary btn-sm">
</div>
</form>
$("#customers-dropdown").change(function() {
var action1 = "/ac/calendar/";
var action2 = "/2014/12/";
var customer = this.value;
var final_action = action1 + customer + action2;
alert(final_action);
$("#switch-customer-form").attr("action", final_action);
});
<强>更新强>
这不会为标记添加值。这样做我 遇到同样的问题,它不会更新表单操作和 只需发布到当前页面。
您被重定向到同一页面,因为更改事件未被触发且表单中没有任何操作。如果将触发更改事件,则操作将更改并且可以正常工作。
您需要向表单添加操作或添加javascript,以便在DOM准备就绪时将操作更改为标记为选中的选项。
例如,在DOM ready上:
var changeAction = function(id) {
var action1 = "/ac/calendar/";
var action2 = "/2014/12/";
var final_action = action1 + id + action2;
$("#switch-customer-form").attr("action", final_action);
};
changeAction($("#customers-dropdown").val());// On document ready, change action to selected item.
$("#customers-dropdown").change(function () {// On change event, change action as well
changeAction(this.value);
});