简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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