使用核心jQuery,如何删除选择框的所有选项,然后添加一个选项并选择它?
我的选择框如下.
<Select id="mySelect" size="9"> </Select>
Run Code Online (Sandbox Code Playgroud)
编辑:以下代码有助于链接.但是,(在Internet Explorer中).val('whatever')未选择已添加的选项.(我没有使用两个相同的"价值" .append和.val.)
$('#mySelect').find('option').remove().end()
.append('<option value="whatever">text</option>').val('whatever');
Run Code Online (Sandbox Code Playgroud)
编辑:试图让它模仿这个代码,每当页面/窗体重置时,我使用以下代码.此选择框由一组单选按钮填充..focus()更接近,但该选项似乎没有像它一样被选中.selected= "true".我现有的代码没有任何问题 - 我只是想学习jQuery.
var mySelect = document.getElementById('mySelect');
mySelect.options.length = 0;
mySelect.options[0] = new Option ("Foo (only choice)", "Foo");
mySelect.options[0].selected="true";
Run Code Online (Sandbox Code Playgroud)
编辑:选择的答案接近我需要的.这对我有用:
$('#mySelect').children().remove().end()
.append('<option selected value="whatever">text</option>') ;
Run Code Online (Sandbox Code Playgroud)
但这两个答案都让我得到了最后的解决方案..
__qualname__python中有什么,它有什么用?
为什么我需要使用它__name__?
我阅读了文档,但它们并没有帮助我清楚地了解它的用处。
我已阅读获取 Python 类的完全限定名称 (Python 3.3+)。
这个问题问的是“如何获得一个合格的名字”,它假定人们知道“合格的名字”的含义。显然,该问题的答案是使用__qualname__属性。
我的问题是什么 __qualname__,为什么我应该在__name__.
考虑使用 OAuth 令牌的 http 请求。访问令牌需要作为不记名包含在标头中。但是,如果令牌已过期,则需要再次请求刷新令牌,然后重试。所以自定义重试对象将如下所示:
s = requests.Session()
### token is added to the header here
s.headers.update(token_header)
retry = OAuthRetry(
total=2,
read=2,
connect=2,
backoff_factor=1,
status_forcelist=[401],
method_whitelist=frozenset(['GET', 'POST']),
session=s
)
adapter = HTTPAdapter(max_retries=retry)
s.mount('http://', adapter)
s.mount('https://', adapter)
r = s.post(url, data=data)
Run Code Online (Sandbox Code Playgroud)
重试类:
class OAuthRetry(Retry):
def increment(self, method, url, *args, **kwargs):
# refresh the token here. This could be by getting a reference to the session or any other way.
return super(OAuthRetry, self).increment(method, url, *args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
问题是刷新token后,HTTPConnectionPool在调用increment后仍然使用相同的header来发起请求。请参阅:https : //github.com/urllib3/urllib3/blob/master/src/urllib3/connectionpool.py#L787。尽管池的实例以增量方式传递,但更改那里的标头不会影响调用,因为它使用标头的本地副本。
这似乎是一个用例,应该经常出现请求参数在重试之间更改。 …
下面的查询是根据插入的行数动态生成的。对于每一行数据,都有一个附加UNION SELECT语句。
INSERT INTO account_sale
(qty, price, product_id)
SELECT $1, $2, t.id
FROM inventory_product AS t
WHERE t.ebay_sku = $3
UNION
SELECT $4, $5, t.id
FROM inventory_product AS t
WHERE t.ebay_sku = $6
...
Run Code Online (Sandbox Code Playgroud)
当我尝试运行查询时,我得到以下信息:
错误:列“qty”的类型为整数,但表达式的类型为文本
此查询的 node.js 页面:
module.exports = async function(orders) {
const pool = require('./config.js');
const client = await pool.connect();
const sql = ...
const data = [
1, 1.50, 10,
2, 4.50, 11
];
client.query(sql, data).then(res => {
...
}).catch(err => console.log(err));
}
Run Code Online (Sandbox Code Playgroud)
UNION …
我想在Mac上将硒与chromedriver一起使用,但遇到了一些麻烦。
import os
from selenium import webdriver
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
DRIVER_BIN = os.path.join(PROJECT_ROOT, "bin/chromedriver_for_mac")
print DRIVER_BIN
browser = webdriver.Chrome(DRIVER_BIN)
browser.get('http://www.baidu.com/')
Run Code Online (Sandbox Code Playgroud)
但是我无法获得想要的结果。
Traceback (most recent call last):
File "/Users/wyx/project/python-scraping/se/test.py", line 15, in <module>
browser = webdriver.Chrome(DRIVER_BIN)
File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
self.service.start()
File "/Users/wyx/project/python-scraping/.env/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver_for_mac' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of …Run Code Online (Sandbox Code Playgroud) 我正在开发一个 Python-3 程序,它试图做两件事:(1)从外部 websocket(非阻塞)读取数据(类型 1)和(2)在常规 UDP 上接收数据(类型 2)套接字(非阻塞)
有很长一段时间,websocket 和 UDP 套接字上都没有数据。因此,我试图对两种数据类型进行非阻塞读取/接收。我正在尝试使用 Asyncio 和 Websockets 为 websocket 执行此操作。
不幸的是,只要 websocket 上没有数据(类型 1),下面的代码就会挂起。它阻止了其余代码的执行。我究竟做错了什么?
在此先感谢您的帮助。
import asyncio
import websockets
import socket
IP_STRATUX = "ws://192.168.86.201/traffic"
# Method to retrieve data (Type 1) from websocket
async def readWsStratux(inURL):
async with websockets.connect(inURL, close_timeout=0.1) as websocket:
try:
data = await websocket.recv()
return data
except websocket.error:
return None
if __name__ == "__main__":
# Socket to receive data (Type 2)
sockPCC = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sockPCC.bind((PCC_IP, PCC_PORT))
sockPCC.setblocking(0)
while …Run Code Online (Sandbox Code Playgroud) 我正在创建一个基于文本的labryinth游戏,但我认为它需要一些图片来说明最新情况.出于某种原因,当我尝试去a2时(图片应该弹出),它就会死掉.我是初学者,所以不要太多地判断我的代码^^
import Image
a1 = ("a1 Go to the start of the Labyrinth", "You're at the start of the labyrinth") #Start
a2 = ("a2 Go Left", "You've chosen to go left, unfortunatly it leads to nothing.") #Fail route
a3 = ("a3 Go Right", "You've chosen to go right.") #Right route
a4 = ("a4 Go Left", "You've chosen to go left") #Fail route
a5 = ("a5 Go Right", "You've chosen to go right") #Right route
a6 = ("a6 Go Right", "You've …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个像这样的简单帖子共享表单。
我正在使用 formset 进行图像上传。但这给了我多种输入,如您所见。每个输入也可以选择单个图像。但我正在尝试使用单个输入上传多个图像。
视图.py
def share(request):
ImageFormSet = modelformset_factory(Images,
form=ImageForm, extra=3)
# 'extra' means the number of photos that you can upload ^
if request.method == 'POST':
postForm = PostForm(request.POST)
formset = ImageFormSet(request.POST, request.FILES,
queryset=Images.objects.none())
if postForm.is_valid() and formset.is_valid():
post = postForm.save(commit=False)
post.author = request.user
post.save()
for form in formset.cleaned_data:
# this helps to not crash if the user
# do not upload all the photos
if form:
image = form['image']
photo = Images(post=post, image=image)
photo.save()
return redirect("index")
else: …Run Code Online (Sandbox Code Playgroud) 我正在尝试找出一种方法,根据后端端点前端的命中情况来制定不同的 CORS 规则。
所以我可以拥有
/api 端点具有 CORS 域白名单和
/public-api 没有 CORS 域白名单。
这是必需的,因为我有两个用于自己前端的内部端点,以及一个可以安装在任何第三方域中的公共 JS 小部件。
我看过django-cors-headers库,但它是正则表达式配置
CORS_ORIGIN_REGEX_WHITELIST = []
Run Code Online (Sandbox Code Playgroud)
致力于让来自域列表的请求通过。
就我而言,我需要一种方法来使用正则表达式(或其他方法)来让对我的端点的请求通过或不通过。
如何在TestCase方法中使用 pytest 固定装置?类似问题的几个答案似乎暗示我的例子应该有效:
import pytest
from django.test import TestCase
from myapp.models import Category
pytestmark = pytest.mark.django_db
@pytest.fixture
def category():
return Category.objects.create()
class MyappTests(TestCase):
def test1(self, category):
assert isinstance(category, Category)
Run Code Online (Sandbox Code Playgroud)
但这总是会导致错误:
TypeError: test1() missing 1 required positional argument: 'category'
Run Code Online (Sandbox Code Playgroud)
我意识到我可以将这个简单的例子转换成一个函数,它会起作用。我更喜欢使用 django,TestCase因为它支持导入传统的“django fixture”文件,这是我的几个测试所需要的。将我的测试转换为函数需要重新实现这个逻辑,因为没有记录的方式使用 pytest(或 pytest-django)导入“django 固定装置”。
包版本:
Django==3.1.2
pytest==6.1.1
pytest-django==4.1.0
Run Code Online (Sandbox Code Playgroud) python ×6
django ×3
python-3.x ×2
casting ×1
cors ×1
django-tests ×1
html-select ×1
image ×1
javascript ×1
jquery ×1
node.js ×1
option ×1
postgresql ×1
pytest ×1
python-2.7 ×1
selenium ×1
sql ×1
urllib3 ×1