我有以下表單,當用戶從<select>
中選擇一個選項時,javascript將其輸入到「名稱」字段中。除非選項包含撇號,否則它只會在撇號之前截斷。我已經嘗試將selectedValue = selectedValue.replace(/'/g, \"\\'\");
添加到javascript,但它沒有幫助。任何意見,將不勝感激。PHP字段條目隨着撇號而變化
#related form objects
echo "<br><br>Name: <input type=\"text\" name=\"name\" id=\"name\" value=\"$name\" size=\"50\">";
echo "<br>Nearby Suggestions: <select name='nearby' id='nearby'>"; $places=file_get_contents('https://maps.googleapis.com/maps/api/place/nearbysearch/json?location='.$lat.','.$long.'&key=xxx&radius=80');
$output= json_decode($places,true);
foreach($output['results'] as $place){
echo "<option value='$place[name]'>$place[name]</option>";
}
echo "</select>";
#script to enter nearby field into name field upon selection
echo "
<script>$(document).on('change','#nearby',function(){
var x = document.getElementById(\"nearby\"),
selectedValue = x.value;
selectedValue = selectedValue.replace(/'/g, \"\\'\");
$('input[name=name]').val(selectedValue);
});</script>";
可能不會解決您的問題,但您應始終在PHP中使用引號包裝字符串。 '$ place [name]'是無效的。它應該始終是'$ place ['name']'或'$ place [「name」]''。必要時使用連接:'「