jquery .val删除间隔文本

时间:2014-07-07 18:48:52

标签: php jquery html mysql

好的,所以我有一个选项来自数据库:

<select id="subject_code" name="subject_code">;
<?php
    $stmt = $database->prepare('SELECT subject_code, subject_name FROM subject_schedule');
    $stmt->execute();
    $result = $stmt->get_result();

    while ($row = $result->fetch_assoc()) {
        $subj_code = $row['subject_code'];
        $subj_name = $row['subject_name'];
        echo "<option value=" . $subj_name . ">" . $subj_code . "</option>";
    }
?>
</select>

一个输入框,其中所选项应放置其值:

<input id="subject_name" type="text" autocomplete="off" placeholder="Subject Name" name="time">

以及它将完成工作的脚本:

$("#subject_code").on('change',function(){
  //get selected option
  var option = $(":selected",this);

  //get its value
  var value = option.val();

  //get input box
  var input = $("#subject_name")[0];

  // set value and disable input
  input.value = value;
});

问题是每当我选择一个有空格的值时,输入框只显示数据库中的第一个单词。 例如:

首选输出:

subject code: ENG111
subject name: Communication Arts 1

当前输出

subject code: ENG111
subject name: Communication

1 个答案:

答案 0 :(得分:4)

问题在于引号:

替换它:

echo "<option value=" . $subj_name . ">" . $subj_code . "</option>";

有了这个:

echo "<option value='" . $subj_name . "'>" . $subj_code . "</option>";

jsFiddle