通过ajax添加选项时,表单未读取select属性

时间:2018-11-12 04:45:39

标签: javascript jquery

我正在像这样通过ajax放置Value | Count 1 | 100000 2 | 300000 3 | 350000 4 | 100000 5 | 50000 6 | 1000 7 | 10

<option>

问题是当我提交表单时,该选项显示未选中,我需要手动选择它,即使我在添加表单时给出了 success: function(data) { jQuery('#usp-cat-combo-1').prepend('<option selected="selected" value="'+data.cat_id+'">'+data.cat_name+'</option>'); }

2 个答案:

答案 0 :(得分:1)

尝试设置Route::post('/siswa', [ 'uses' => 'SiswaController@store', 'as' => 'simpan_siswa' ]); 的值,而不是在选项上设置所选属性

(defn map-vals
  "Map f over every value of m.
   Returns a map with the same keys as m, where each of its values is now the result of applying f to them one by one.
   f is a function of one arg, which will be called which each value of m, and should return the new value.
   Faster then map-vals-transient on small maps (8 elements and under)"
  [f m]
  (reduce-kv (fn [m k v]
               (assoc m k (f v)))
             {} m))

(defn map-vals-transient
  "Map f over every value of m.
   Returns a map with the same keys as m, where each of its values is now the result of applying f to them one by one.
   f is a function of one arg, which will be called which each value of m, and should return the new value.
   Faster then map-vals on big maps (9 elements or more)"
  [f m]
  (persistent! (reduce-kv (fn [m k v]
                            (assoc! m k (f v)))
                          (transient {}) m)))

请注意,这假设<select>不是jQuery('#usp-cat-combo-1') .prepend('<option value="'+data.cat_id+'">'+data.cat_name+'</option>'); .val(data.cat_id) 。如果是倍数,可以在新的第一个孩子上设置<select>

答案 1 :(得分:-1)

您需要做什么:

默认定义要选择的选项,例如:

var selected;
success: function(data) {
  data.cat_id=='1' ? selected = 'selected="selected"' : selected = '';
  jQuery('#usp-cat-combo-1').prepend('<option '+selected+' value="'+data.cat_id+'">'+data.cat_name+'</option>');
}

现在,只需在您未定义的选项中添加已选择 属性即可。

注意:

  1. 假设您具有作为数组的数据。
  2. 您可以根据需要使用 data.cat_name 或其他名称来代替 cat_id 来定义您的 Selected Option

随时发表评论以寻求更多帮助或其他简化:)