简体   繁体   English

jQuery .val删除空格文本

[英]jquery .val remove spaced text

ok, so i have a select items in which the option comes from the database: 好的,所以我有一个选择项,其中选项来自数据库:

<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>

an input box where the selected item should put its value: 一个输入框,其中所选项目应放置其值:

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

and a script where it will do the job: 以及执行任务的脚本:

$("#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;
});

the problem is whenever i select a value in which it has spaces on them, the input box only display the first word from the database. 问题是,每当我选择一个在其上具有空格的值时,输入框仅显示数据库中的第一个单词。 example: 例:

preferred output: 首选输出:

subject code: ENG111
subject name: Communication Arts 1

current output 电流输出

subject code: ENG111
subject name: Communication

The problem is with quotes: 问题在于引号:

Replace this: 替换为:

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

With this: 有了这个:

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

jsFiddle 的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM