亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試從 select 標記中獲取值時返回的 URL

嘗試從 select 標記中獲取值時返回的 URL

郎朗坤 2021-12-08 16:13:15
我是flask和 的新手Javascript。我想要做的就是創建一個帶有動態下拉列表的表單,獲取選擇的值并將它們放入數據庫中?,F在我處于創建動態下拉列表的第一階段。這是代碼的我的python部分。class Form(FlaskForm):    site = SelectField('Site', choices=[(' ', ' '), ('International', 'International'), ('Domestic', 'Domestic')])    location = SelectField('Location', choices=[])    city = SelectField('City', choices=[])    street = SelectField('Street', choices=[])    sub_street = SelectField('BuildingStreet', choices=[])    floor = SelectField('Floor', choices=[])    room = SelectField('Room', choices=[])    side = SelectField('Side', choices=[])@app.route('/')def basic():    return render_template('basic.html')@app.route('/Welcome', methods=['GET', 'POST'])def index():    form = Form()    location_choice_list = [(locations.id, locations.location) for locations in                            Hostname.query.filter_by(site='International').all()]    form.location.choices = remove_duplicates.unique_list(location_choice_list)    city_choice_list = [(cities.id, cities.city) for cities in Hostname.query.filter_by(site='International').all()]    form.city.choices = remove_duplicates.unique_list(city_choice_list)    street_choice_list = [(streets.id, streets.street) for streets in                          Hostname.query.filter_by(site='International').all()]    form.street.choices = remove_duplicates.unique_list(street_choice_list)    sub_street_choice_list = [(sub_streets.id, sub_streets.sub_street) for sub_streets in                              Hostname.query.filter_by(site='International').all()]    form.sub_street.choices = remove_duplicates.unique_list(sub_street_choice_list)所以首先我創建了一個類,其中包含用戶可以從表單中選擇的所有下拉列表。除了站點之外,所有其他選項最初都填充有對應于“國際”的選項。即“國際”對應的所有地點,“國際”對應的所有城市等等。我使用了一個小函數作為模塊來刪除數據庫中無法避免的重復條目。最初,當顯示表單時,一切都按預期進行。我沒有放置 basic.html 文件,因為它只包含一個指向“/Welcome”的鏈接。此外,我用作模塊的 remove_duplicates 函數也沒有問題。
查看完整描述

1 回答

?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

您的location變量與window.location使用當前文檔 url- https://developer.mozilla.org/en-US/docs/Web/API/Window/location的變量沖突


var list = [{"id":" ","location":" "}, {"id":"CN0","location":"CN0"},{"id":"India","location":"India"},{"id":"Japan","location":"Japan"},{"id":"Honkong","location":"Honkong"},{"id":"GB0","location":"GB0"}];


const countries = document.querySelector('#countries');

let optionHTML = ""; 


list.forEach((obj) => {

    optionHTML += '<option value="' + obj.id + '">' + obj.location + '</option>';

});


countries.innerHTML = optionHTML;


countries.addEventListener('change', function() {

    location = this.value;

    // alert(location);

    alert(window.location === location);

});

<select name="" id="countries"></select>

而是使用不同的變量,而不是location使用block-scoped變量 usinglet或const未提升且不與全局變量沖突的變量。


var list = [{"id":" ","location":" "}, {"id":"CN0","location":"CN0"},{"id":"India","location":"India"},{"id":"Japan","location":"Japan"},{"id":"Honkong","location":"Honkong"},{"id":"GB0","location":"GB0"}];



const countries = document.querySelector('#countries');


let optionHTML = ""; 


list.forEach((obj) => {

    optionHTML += '<option value="' + obj.id + '">' + obj.location + '</option>';

});


countries.innerHTML = optionHTML;


countries.addEventListener('change', function() {

    let location = this.value;

    console.log(location);

});

<select name="" id="countries"></select>


查看完整回答
反對 回復 2021-12-08
  • 1 回答
  • 0 關注
  • 267 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號