با سلام من می خوام چند اینپوت اضافه کنم با تایپ radio می خوام یک کاری کنم که value اونی که checked است رو بگیرم چجوری اگه میشه بگین
سلام اگه میخوای با فرمای جنگو اینکارو انجام بدی میتونی از widget . forms.RadioSelect استفاده کنی به این صورت
CHOICES = [('accessName1', 'ShowValue1'),
('accessName2', 'ShowValue2')]
test = forms.ChoiceField(choices=CHOICES,error_messages={'invalid_choice':'نوع انتخاب معتبر نمیباشد'},widget=forms.RadioSelect(attrs={'class': 'TEST'}),
)اگه با جاوا میخوای اینکارو کنی
یه سری اینپوت تعریف کن
<input type="radio" name="customradio" value="گزینه اول">
<input type="radio" name="customradio" value="گزینه دوم">تو فایل js هم میتونی با jquery مقدار انتخاب شدرو بدست بیاری
const SelectedRadio= $('input[name="customradio"]:checked').val();
ممنون 👌👌👌🙏🙏🙏🙏
خواهش میکنم 😉


چرا من روی هر کدوم که میزنم در اینپکت checked نمی خوره
کد html
{% for color in colors %}
<label>{{ color }}</label> <input id='color-product' type="radio" name="customradio" value="{{ color }}">
{% endfor %}کد js
const productCount = $('#product-count').val();
const SelectedRadio= $('input[name="customradio"]:checked').val();
$.get('/order/add-to-order?product_id=' + productId + '&count=' + productCount + '&color=' + SelectedRadio)id هاشون یکیه برای همین تداخل میخوره برای درست کردنش id رو برابر color-product{color.id}
قرار بدی مشکل درست میشه
راجب اینکه چرا checked نمیاره راستش نمیدونم برا منم اینجوریه
نه درست نشد 😔😔😔
اطلاعات به درستی انتقال داده میشه؟ ( رنگی که انتخاب میکنی درست فرستاده میشه؟)
نه چون اصلا checked نمیشه که درست فرستاده بشه
عکس ویو add-to-order رو میدی لطفا چون برا منم checked نمیاره ولی کار میکنه فکرم نکنم مهم باشه
def add_product_to_order(request):
product_id = int(request.GET.get('product_id'))
count = int(request.GET.get('count'))
color_product = request.GET.get('color')
if count < 1 or color_product is '':
return JsonResponse({
'status': 'invalid_count'
})
elif count > 4 :
return JsonResponse({
'status': 'invalid_count'
})
if request.user.is_authenticated:
if request.user.address is None:
return JsonResponse({
'status': 'is_none_res'
})
if request.user.mobile is None or request.user.address_ip is None or request.user.address is None :
return JsonResponse({
'status': 'is_none_res'
})
product = Product.objects.filter(id=product_id, is_active=True, is_delete=False).first()
if product is not None:
# current_order = Order.objects.filter(is_paid=False, user_id=request.user.id).first()
current_order, created = Order.objects.get_or_create(is_paid=False, user_id=request.user.id)
current_order_detail = current_order.orderdetail_set.filter(product_id=product_id).first()
if current_order_detail is not None:
current_order_detail.count += count
current_order_detail.color = color_product
current_order_detail.save()
else:
new_detail = OrderDetail(order_id=current_order.id, product_id=product_id, count=count, color=color_product)
new_detail.save()
return JsonResponse({
'status': 'success'
})
else:
return JsonResponse({
'status': 'not_found'
})
else:
return JsonResponse({
'status': 'not_auth'
})بعد خط چهارم که این هست
color_product = request.GET.get('color')میشه یه print بگیری از color ایی که دریافت میشه
print(color_product)موقع ثبت از run ide هم عکس میدی اگه اروری میده با console log مرورگر
رو سرور کجا پرینت می کنه
تازه وقتی که میزنم رو افزودن به سبد خرید داخل سبد خرید قسمت رنگ میاره undefined
پابلیشه؟ که اینطور
به نظر میاد کار میکنه فقط تو گرفتن اطلاعات رنگ مشکل هست که اینارو امتحان کن ببین چی میشه
اگه undefined هست فقط یه چیزی مدل color رو چجوری ساختی اینجوری
class ProductColors(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True)
title = models.CharField(max_length=100)یا فقط یه فیلد معمولی تو order detail ساختین
اگه به روش اول ساختین توی تمپلیت یجور دیگه اطلاعات رنگ انتخاب شدرو بفرستین
{% for color in colors %}
<label>{{ color }}</label>
<input id='color-product' type="radio" name="customradio" value="{{ color.id }}">
{% endfor %}و توی view add to order
اون قسمتی که محصولو دریافت میکنی با id رنگ هم دریافت کن
product = Product.objects.filter(id=product_id, is_active=True, is_delete=False).first()
the_color = ProductColors.object.filter(id=color_product).first()
if product is not None and the_color is not None:
قسمت دوم نفهمیدم دقیقا باید چکار کنم
خب ببین من کاری که میخوایو رو پروژه خودم تست کردم کار کرد قبل هر کاری یه cntrl f5 رو امتحان کن
(قسمت دوم value رو به id رنگ تغییر دادم که نیازی نیست انجام بدی اگه مدلی نساختی و یه فیلد سادس تو orderDetail)
اگه نشد از راه مدل امتحانش کن
کنترل f5 زدم درست شد خیلی ممنون 🙏🙏🙏🙏🙏🙏🙏🙏
خوشحال شدم فقط یه چیز دیگه ایین قسمت از کدت که
current_order_detail = current_order.orderdetail_set.filter(product_id=product_id).first() if current_order_detail is not None:رو به این تغییر بده که بتونی از یه محصول با رنگای مختلف بتونی تو سبد خریدت داشته باشی
current_order_detail = current_order.orderdetail_set.filter(product_id=product_id, color=color_product).first()
ممنون 🙏🙏🙏🙏🙏
سلام دوست من
خوشحالم که مشکلتون برطرف شده
ممنونم از اقای اکبری بابت کمکشون
موفق باشین :)